package cn.yiya.shiji.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.duowan.mobile.netroid.image.NetworkImageView;

import java.util.ArrayList;

import cn.yiya.shiji.R;
import cn.yiya.shiji.activity.SingleBrandOrderActivity;
import cn.yiya.shiji.entity.goodes.NewGoodsItem;
import cn.yiya.shiji.netroid.Netroid;
import cn.yiya.shiji.utils.SimpleUtils;
import cn.yiya.shiji.utils.Util;

/**
 * Created by chenjian on 2015/11/16.
 */
public class HorRecyclerViewAdapter extends RecyclerView.Adapter<HorRecyclerViewAdapter.HorizontalGoodsViewHolder>{

    private Context mContext;
    private ArrayList<NewGoodsItem> mLists;
    private int GOODS = 1;
    private int MORE = 2;
    private int id;
    private OnItemClickListener onItemClickListener;

    public HorRecyclerViewAdapter(Context mContext, ArrayList<NewGoodsItem> mLists, int id) {
        this.mContext = mContext;
        this.mLists = mLists;
        this.id = id;
    }

    @Override
    public HorizontalGoodsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == GOODS) {
            return new HorizontalGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_itme_newest, parent, false));
        } else {
            return  new HorizontalGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.brands_more_goods, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(HorizontalGoodsViewHolder holder, final int position) {
        if (position == mLists.size()) {
            holder.cvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((SingleBrandOrderActivity)mContext).setCurTabToGoods(id);
                }
            });
        }else {
            final NewGoodsItem info = mLists.get(position);
            int nWidth = SimpleUtils.dp2px(mContext, 85);
            if (!TextUtils.isEmpty(info.getCover())) {
                Netroid.displayImage(Util.transferCropImage(info.getCover(), nWidth), holder.ivImg);
            }
            holder.ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    GoodsIdInfo goodsIdInfo = new GoodsIdInfo();
//                    goodsIdInfo.setGoodsId(mLists.get(position).getId());
                    if(onItemClickListener != null){
                        onItemClickListener.OnItemClick(info);
                    }
//                    Intent intent = new Intent(mContext, GoodsDetailActivity.class);
//                    intent.putExtra("data", new Gson().toJson(goodsIdInfo));
//                    mContext.startActivity(intent);
                }
            });
            holder.tvTxt.setText("￥" + info.getPrice());
        }
    }

    @Override
    public int getItemCount() {
        return mLists.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mLists.size()) {
            return MORE;
        }else {
            return GOODS;
        }
    }

    public interface OnItemClickListener{
        void OnItemClick(NewGoodsItem data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    class HorizontalGoodsViewHolder extends RecyclerView.ViewHolder {
        TextView tvTxt;
        NetworkImageView ivImg;
        CardView cvMore;

        public HorizontalGoodsViewHolder(View itemView) {
            super(itemView);
            tvTxt = (TextView) itemView.findViewById(R.id.newest_tx);
            ivImg = (NetworkImageView) itemView.findViewById(R.id.newest_iv);
            cvMore = (CardView) itemView.findViewById(R.id.more_card);
        }
    }
}


